<?php

namespace app\common\logic;

use think\Db;

/**
 * 课程标签操作
 * Class SendLogic
 */
class CourseTagsLogic extends Logic
{
    public static function process()
    {
        $course_relation_db = Db::name('course_relation');
        $course_list = Db::name('course')->where('is_publish', 1)->field('id,tags')->select()->toArray();

        $relation = $course_relation_db->column('id', 'keys');
        foreach ($course_list as $k => $v) {
            foreach (explode(',', $v['tags']) as $vv) {
                if (empty($vv)) continue;

                $keys = md5($v['id'] . '#' . $vv);
                if (!isset($relation[$keys])) {
                    $course_relation_db->insert([
                        'course_id' => $v['id'],
                        'tags' => $vv,
                        'keys' => $keys,
                        'add_time' => time(),
                    ]);
                }
            }

        }
    }

    public static function course_tags()
    {
        $page = 0;
        $count = 30;

        $exist_keys = Db::name('course_search')->column('id', 'keys');

        //课程标签
        $course_list = Db::name('course')->alias('c')
            ->join('tp_teacher t', 'c.teacher_id = t.id', 'left')
            ->field('c.title course_name,o.name org_name,o.id org_id,c.id,t.name teacher_name,t.id teacher_id')
            ->order('c.add_time desc')
            ->limit($page, $count)
            ->select()->toArray();
        $add_item = [];
        foreach ($course_list as $v) {

            if (mb_strlen($v['course_name']) > 30) continue;
            $key1 = md5($v['course_name'] . '#' . $v['id'] . '#1');
            if (!isset($exist_keys[$key1])) {
                array_push($add_item, [
                    'course_id' => $v['id'],
                    'tags' => $v['course_name'],
                    'type' => 1,
                    'keys' => $key1,
                    'add_time' => time(),
                ]);
            }

            if (mb_strlen($v['org_name']) > 30) continue;
            $key1 = md5($v['org_name'] . '#' . $v['id']);
            if (!isset($exist_keys[$key1])) {
                array_push($add_item, [
                    'course_id' => $v['id'],
                    'tags' => $v['org_name'],
                    'type' => 3,
                    'keys' => $key1,
                    'add_time' => time(),
                ]);
            }

            if (mb_strlen($v['teacher_name']) > 30) continue;
            $key1 = md5($v['teacher_name'] . '#' . $v['id'] . '#' . $v['teacher_id'] . '#4');
            if (!isset($exist_keys[$key1])) {
                array_push($add_item, [
                    'course_id' => $v['id'],
                    'tags' => $v['teacher_name'],
                    'type' => 4,
                    'keys' => $key1,
                    'add_time' => time(),
                ]);
            }
        }
        if (count($add_item) > 0) Db::name('course_search')->insertAll($add_item);

        //章节标签
        $lesson_list = Db::name('lesson')
            ->field('id,title,course_id')
            ->order('add_time desc')
            ->limit($page, $count)
            ->select()->toArray();
        $add_item = [];
        foreach ($lesson_list as $v) {

            if (mb_strlen($v['title']) > 30) continue;
            $key1 = md5($v['title'] . '#' . $v['course_id'] . '#' . $v['id'] . '#2');
            if (!isset($exist_keys[$key1])) {
                array_push($add_item, [
                    'course_id' => $v['course_id'],
                    'tags' => $v['title'],
                    'type' => 2,
                    'keys' => $key1,
                    'add_time' => time(),
                ]);
            }
        }
        if (count($add_item) > 0) Db::name('course_search')->insertAll($add_item);

        //视频标题标签
        $video_list = Db::name('video')->alias('v')
            ->join('tp_lesson_res r', 'r.res_id = v.id', 'left')
            ->join('tp_lesson l', 'r.lesson_id = l.id', 'left')
            ->field('v.id,v.title,l.course_id')
            ->order('v.add_time desc')
            ->limit($page, $count)
            ->select()->toArray();
        $add_item = [];
        foreach ($video_list as $v) {
            if (mb_strlen($v['title']) > 30) continue;
            $key1 = md5($v['title'] . '#' . $v['course_id'] . '#' . $v['id'] . '#5');
            if (!isset($exist_keys[$key1])) {
                array_push($add_item, [
                    'course_id' => $v['course_id'],
                    'tags' => $v['title'],
                    'type' => 5,
                    'keys' => $key1,
                    'add_time' => time(),
                ]);
            }
        }
    }
}